package com.hmall.item.listener;

import com.alibaba.fastjson.JSON;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * x
 *
 * @Description SpringAMQPListener
 * @Author kzw
 * @Date 2024-11-14
 */
@Slf4j
@Component
public class SpringAMQPListener {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 简单模型-消费者
     *
     * @param status
     */
    @RabbitListener(queues = "itemStatus.queue")
    public void simpleListener(Integer status) {
        String itemStatus = status == 0 ? "下架" : "上架";
        log.info("simpleListener 接收到消息：商品" + itemStatus + "重新加载Top20数据");
        List<Item> topProducts = itemMapper.searchListTop();
        // 将销量 Top20 的商品列表保存到 Redis 中，使用一个特定的键
        redisTemplate.opsForValue().set("top20", JSON.toJSONString(topProducts));
    }
}
